﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace SalesForecastOFR
{
    partial class SalesForecast
    {
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
        [Microsoft.Office.Tools.Outlook.FormRegionName("SalesForecastOFR.SalesForecast")]
        public partial class SalesForecastFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void SalesForecastFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
                try
                {
                    Outlook.MailItem mail = (Outlook.MailItem) e.OutlookItem;
                    // Saving the file doesn't persist the file name to the attachment... so need to track it manually.
                    if (mail.Attachments.Count != 1)
                    {
                        e.Cancel = true;
                        return;
                    }
                    foreach (Outlook.Attachment att in mail.Attachments)
                    {
                        if (att.DisplayName != "LitwareForeCastData.xml")
                        {
                            e.Cancel = true;
                        }
                    }
                }
                catch
                {
                    e.Cancel = true;
                }
            }
        }

        #endregion


        // Occurs when the form region is being displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void SalesForecast_FormRegionShowing(object sender, System.EventArgs e)
        {
            Outlook.MailItem mail = (Outlook.MailItem) this.OutlookItem;
            string path = "C:\\OFRTemp\\LitwareForeCastData.xml";
            foreach (Outlook.Attachment att in mail.Attachments)
            {
                // Again ideal scenario this goes to a temporary or cache directory... however, for
                // development purposes it makes sense to make this easily accessible so that a developer
                // can review the attachment content.  If you need to support more then one attachment
                // then you'll need a unique name for each - that type of standard file management logic.
                att.SaveAsFile(path);
            }
            if (this.elementHost1.Child == null)
                this.elementHost1.Child = new Charting.EmailControl(path);
        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void SalesForecast_FormRegionClosed(object sender, System.EventArgs e)
        {
        }
    }
}
